package com.bacel.nb.product.web.controller;

import com.alibaba.fastjson.JSON;
import com.bacel.nb.product.core.domain.App;
import com.bacel.nb.product.core.domain.Device;
import com.bacel.nb.product.core.domain.Sysuser;
import com.bacel.nb.product.core.dto.ListGageModule;
import com.bacel.nb.product.core.service.*;
import com.bacel.nb.product.web.template.ViewModel.FreeMarkerView;
import com.bacel.nb.product.web.util.AddToRedis;
import com.bacel.nb.product.web.util.WebUtilify;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Bacel on 2017/05/01.
 */
@RestController
@RequestMapping(value = "/sysmanage/businessplatform", produces = "text/html;charset=UTF-8")
public class businessplatformController {
    private final Logger logger = Logger.getLogger(businessplatformController.class);

    @Autowired
    private IAppService moduleService;
    @Autowired
    private ISysmoduleService sysmoduleService;
    @Autowired
    private IDeviceService deviceService;
    @Autowired
    private ILogsService logsService;
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public final ModelAndView list(final HttpSession session, final HttpServletRequest request)
            throws Exception {
        String path = request.getContextPath();
        String menulist = WebUtilify.BuildMenuinfo(path,sysmoduleService);
        // 显示首页
        ModelAndView mav = new FreeMarkerView("businessplatform/list", "第三方业务平台信息", path, menulist, session);
        return mav;
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public final ModelAndView add(final HttpSession session, final HttpServletRequest request)
            throws Exception {
        String ports = String.valueOf(request.getServerPort());
        String  sn = request.getParameter("id");
        System.out.println(sn);
        if (!ports.equals("80")) {
            ports = ":" + ports;
        }
        String basePath = request.getScheme() + "://" + request.getServerName() + ports;
        // 显示首页
        ModelAndView mav = new FreeMarkerView("businessplatform/add", "编辑", "", basePath + "/sysmanage/businessplatform/list", session);
        return mav;
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public String edit(@RequestBody App module, final HttpServletRequest request) {
        String ret = "";
        String errstr = "";
        try {
            if (module != null && module.getAppname() != null) {

                if (module.getCreatedate() == null)
                    module.setCreatedate(new Timestamp(System.currentTimeMillis()));

                if (module.getId() != null && module.getId() > 0) {
                    this.moduleService.updateT(module);
                    Device device=new Device();
                    device.setAppByAppid(module);
                    List<Device> list = deviceService.findByExampleEq(device);
                    AddToRedis.addToRedis(list);
                } else {
                    this.moduleService.insertT(module);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            errstr = "操作失败001";
        }
        ret = WebUtilify.ReturnJsoninfo(errstr);
        return ret;
    }

    @RequestMapping(value = "/listdata", method = RequestMethod.GET)
    public String listdata(final HttpServletRequest request)
            throws Exception {

        int pagesize = WebUtilify.parseInt(request.getParameter("rows"), 15);
        int pageindex = WebUtilify.parseInt(request.getParameter("page"), 1);
        Sysuser user = WebUtilify.LoginUserinfo(request);
        ListGageModule datamodule = new ListGageModule<>();
        datamodule.setPagesize(pagesize);
        datamodule.setPageindex(pageindex);
        App app=new App();
        if (user.getSysroleByRoleid().getId()!=1){
            app.setSysuserByUserid(user);
        }
        moduleService.getPageByExampleEq(app,datamodule);
        Map<String, Object> info = new HashMap<>();
        info.put("records", datamodule.getTotalcount());
        info.put("total", datamodule.getPagecount());
        info.put("rows", datamodule.getList());

        String ret = "";
        try {
            ret = JSON.toJSONString(info);// JSONArray.toJSONString(datamodule);
        } catch (Exception ex) {
            System.out.println("moduledata:" + ex.toString());
        }
        return ret;
    }

    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public String del(final HttpServletRequest request) {
        String ret = "";
        String errstr = "";
        Integer delid = Integer.parseInt(request.getParameter("id"));
        if (delid > 0)
            this.moduleService.deleteByIdT(delid);
        else {
            errstr = "传递参数有误";
        }

        ret = WebUtilify.ReturnJsoninfo(errstr);
        return ret;
    }

    @RequestMapping(value = "/getmodule", method = RequestMethod.GET)
    public String getmodule(final HttpServletRequest request)
            throws Exception {
        Integer id = Integer.parseInt(request.getParameter("id"));
        App module = this.moduleService.get(id);
        Map<String, Object> info = new HashMap<>();
        info.put("module", module);
        return JSON.toJSONString(info);
    }
}